#light

open Langexplr.Abc
open Langexplr.Abc.InstructionUtilities
open System.IO


let abcFileCreator = AbcFileCreator()
let cpCreator = abcFileCreator.ConstantPoolCreator

let instructions =
   [|  GetLocal0 ;
       PushScope ;
       FindPropertyStrict(CQualifiedName(Ns("",NamespaceKind.Namespace),"print"));
       PushString "Hola!";
       CallProperty(CQualifiedName(Ns("",NamespaceKind.Namespace),"print"),1) ;
       CoerceA  ;
       SetLocal_1 ;
       GetLocal1 ;
       ReturnValue ;
       Kill(1);
   |];

//let code = ConvertToByteArray(instructions,abcFileCreator)

let script = 
   AvmScript(
      AvmMethod( "",
                 SQualifiedName("*"),
                 [||],
                 Some (
                   AvmMethodBody( 2, 2, 1, 2,
                                  instructions,
                                  [||],[||]
                      ))),[]); 

let _ = abcFileCreator.AddScript(script.ToLowerIr(abcFileCreator))

let fileRep = abcFileCreator.CreateFile() 

using (new BinaryWriter(new FileStream("Hello.abc",FileMode.Create))) (fun f -> fileRep.WriteTo(f))

System.Console.WriteLine("Done!");


let loadedFile =
      using(new BinaryReader(new FileStream("Hello.abc",FileMode.Open))) 
           (fun aInput -> AbcFile.ReadFrom(aInput))

System.Console.WriteLine(loadedFile.Scripts.Length) 

let loadedScript = 
    AvmScript.Create(loadedFile.Scripts.[0],
                     loadedFile.Methods,
                     loadedFile.MethodBodies,
                     loadedFile.Instances,
                     loadedFile.ConstantPool,[||]);

System.Console.WriteLine(loadedScript.InitMethod.ReturnType)
System.Console.WriteLine(loadedScript.InitMethod.Body.IsSome)
